package com.magicsoft.share.ui.my;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.UserAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.UserList;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.customview.alertview.OnItemClickListener;
import com.magicsoft.share.presenter.my.FollowFansPresenter;
import com.magicsoft.share.ui.share.HomePageActivity;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.my.IFollowFansView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

public class FollowActivity extends BaseActivity<IFollowFansView, FollowFansPresenter> implements IFollowFansView {

    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;

    private UserAdapter userAdapter;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;

    @Override
    public FollowFansPresenter initPresenter() {
        return new FollowFansPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_follow;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvTitle.setText("关注");
        initRV();

    }


    @Override
    public void requestData() {
        mPresenter.followFansList("1", pageNo, pageSize);
    }


    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void followFansListSuccess(UserList data) {

        if (isRefresh) {
            userAdapter.setNewData(data.getList());
        } else {
            userAdapter.addData(data.getList());
        }

        if (pageNo < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNo==data.getPages()){
            recyclerView.loadMoreFinish(false, false);
        }else {
            recyclerView.loadMoreFinish(true, false);
        }



    }

    @Override
    public void followFansListFail() {

        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }

    @Override
    public void followChangeSuccess() {

        requestData();
    }

    private void initRV() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(this, 0.5f)));


        recyclerView.useDefaultLoadMore();
        recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNo++;
                requestData();
            }
        });


        userAdapter = new UserAdapter(R.layout.item_user, 1);
        userAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.btn) {
                    UserList.ListBean item = userAdapter.getItem(position);
                    long otherUserId = item.getAppUserId();

                    initDialog(otherUserId);
                } else if (view.getId() == R.id.iv_head) {
                    Intent intent = new Intent(FollowActivity.this, HomePageActivity.class);
                    intent.putExtra("appUserId",userAdapter.getItem(position).getAppUserId());
                    startActivity(intent);
                }
            }
        });
        recyclerView.setAdapter(userAdapter);


    }

    private void initDialog(final long otherUserId) {
        new AlertView.Builder().setTitle("确认不再关注此人？").
                setContext(this).setCancelText("取消").setOthers(new String[]{"确认"}).
                setStyle(AlertView.Style.Alert).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

                if (position == 0) {
                    mPresenter.deleteFollow(otherUserId);
                }
            }
        })
                .build().
                show();
    }

}
